Jim's
Tutorials

Fall 2018
course
site

Current Status

I've mostly just been going through the course and thinking about how it relates to my project. So far I've worked with some of the tools, set up a testing environment and pushed some basic contracts on to the rinkeby test network. Below is an overview of the things I've been looking at. Creating a rock-paper-scissors contract to work as a referee for a game will be a little more involved, but I'm starting to get some idea of how to do it. Handling the entering of the event and sending of ether to the winner should be pretty straightforward anyway - rather similar to the vote contract at the bottom of the page. Figuring out who won in an un-exploitable manner could be a bit more of a challenge.

Remix

https://remix.ethereum.org/

Remix is a browser-based IDE that lets you write and deploy smart-contracts to a javascript in-browser test environment, a test etherium network or the main etherium network. You can tell it's still in heavy development because at the outset you can choose from a long and often updated list of versions to compile and run. Super useful tool for composing and testing contracts. The browser-based test environment creates a number of different accounts populated with fake eth to deploy and make requests from your contract. There's also a GDB-style debugging mode that lets you step through your code.

Metamask

Metamask is a chrome browser extension that lets you interact easily with the etherium network. It's essentially a wallet that lets you send and receive eth and other ERC-20 tokens on the etherium network. I found it interesting that it creates a 12-word mnemonic pass-key for you and then creates a large number of unique accounts that can be recovered using the phrase.

Ganache

Ganache is used to create a virtual etherium blockchain and generate a number of fake accounts to use for testing purposes. I learned to pair it with Mocha and Web3 to create tests for an etherium contract.

Web3.js/py/java

Web3 is currently the only way to interact with the etherium network from client-facing applications. I used the Web.js API but there's one for quite a number of languages. It is a library that lets you do things like transfer eth and tokens, read and write data from contracts, create smart contracts and much more.

Solidity

Solidity it the language for writing etherium smart contracts. It looks a lot like a strongly typed version of javascript. Variables and functions are public or private and return values have to be explicitly specified. There are some odd idiosyncrasies, for instance - strings are treated as dynamic arrays and nested dynamic arrays cannot be deployed, so you can't successfully deploy an array of strings, or any other slightly less common nested dynamic array. The language itself also has some interesting built-in functionality - like an ether keyword to automatically translate between different currency denominations and a msg structure (there aren't objects in solidity - more reminiscent of c) that contains information such as the senders address, amount of ether being transferred and a few other things related to each transaction with the contract.

Code

The contract written in solidity at Inbox.sol is pretty simple - It has a message and that message can be changed or read. Making message a public variable automatically creates a function that returns the message, much like an attr: reader in ruby. To deploy the contract to the etherium network the code must first be compiled into byte-code. To do that we are using the javascript solc compiler in compile.js. The compiler outputs 2 things - the bytecode and an ABI or Application Binary Interface, which is a map for function calls relating to the contract. The deployment code utilizes the Web3 library and an HDWalletProvider. The wallet provider accepts the address, or passphrase, of a wallet and the address of an etherium node. One could run an entire node to deploy contracts or utilize the infura service to connect remotely to a node through their API as I did. Once the contract is deployed there's 2 ways to interact with it. You can call a function that returns data without modifying anything instantly and for free, and you can make a transaction that modifies data for a gas fee.

example contract

pragma solidity ^0.4.17;

 contract Lottery {
     address public manager;
     address[] public players;

     function lottery() public{
         manager = msg.sender;
     }

     function enter() public payable{
         require(msg.value > .01 ether);

         players.push(msg.sender);
     }

    function random() private view returns (uint){
       return  uint(keccak256(block.difficulty, now, players));
    }

    function pickWinner() public {
        uint index = random() % players.length;
        players[index].transfer(this.balance);

        players = new address[](0);
    }

 }

This is a contract to mimick a lottery. It's not 100% complete but it essentially functions. There's a function that allows you to enter the lottery, requires a minimum payment, and stores your address in an array of addresses. There's a function that selects a completely deterministic, psuedo-random number based on hashing the block difficulty, the time, and the array of entered players. Finally there is a function that selects a random winner, then transfers the balance of the contract (this.balance) to their account. It then resets the player array at 0 players. This could use a few security measures like only allowing the creator of the lottery (the manager) to pick a random winner and a better random number generator, but it mostly works.

attachments [paper clip]

  last modified size
TXT Inbox.sol Sun May 05 2024 08:26 pm 252B
TXT compile.js Sun May 05 2024 08:26 pm 270B
TXT deploy.js Sun May 05 2024 08:26 pm 770B
TXT inbox.test.js Sun May 05 2024 08:26 pm 1.5K